package com.bw.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSON;
import com.bw.entity.AssetsInfo;
import com.bw.entity.Dict;
import com.bw.mapper.AssetsInfoMapper;
import com.bw.service.AssetsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AssetsInfoServiceImpl implements AssetsInfoService {


    @Autowired
    AssetsInfoMapper assetsInfoMapper;


    @Override
    public AssetsInfo add(AssetsInfo assetsInfo) {
        // 非空验证
        if (assetsInfo.getName() == null || assetsInfo.getName().equals("")){
            throw new RuntimeException("资产名称不为空");
        }
        // hutool 工具类   断言（断定）
        Assert.notBlank(assetsInfo.getName(),"资产名称不为空");

        Assert.notNull(assetsInfo.getTypeId(),"资产类别不为空");
        Assert.notNull(assetsInfo.getSourceId(),"资产来源不为空");
        Assert.notBlank(assetsInfo.getPrice(),"价格不为空");

        String num = assetsInfo.getNum();
        AssetsInfo assetsInfoDB =   assetsInfoMapper.selectByNum(num);

        // 断定 这个入参是null，  如果不是null  就抛异常
        Assert.isNull(assetsInfoDB,"资产编号重复");

//        if (assetsInfoDB != null){
//            throw new RuntimeException("资产编号重复");
//        }

        String price = assetsInfo.getPrice();

        boolean number = NumberUtil.isNumber(price);
        if (!number){
            throw new RuntimeException("价格必须为数值");
        }
        System.out.println("方法的参数：" + JSON.toJSONString(assetsInfo));

        assetsInfoMapper.add(assetsInfo);

        System.out.println("添加之后的主键id是" + assetsInfo.getId());


        return assetsInfo;

    }

    @Override
    public AssetsInfo select(String num) {
        AssetsInfo assetsInfo = assetsInfoMapper.selectByNum(num);
        // 查询下拉框里的值
       List<Dict> sourceList =  assetsInfoMapper.selectSource();
        assetsInfo.setSourceList(sourceList);
       List<Dict> typeList =  assetsInfoMapper.selectType();
        assetsInfo.setTypeList(typeList);
        return assetsInfo;
    }
}
